#!/usr/bin/ruby

load 'comm.rb'

class Pokers
	#Hearts:红桃,Diamonds:方块,Spades:黑桃,Clubs:梅花
	CARD_TYPE = %w[红桃 方块 黑桃 梅花]
	#CARD_TYPE = %w[红桃 方块 梅花]
	@@ct = Ctxt.new
  
	def initialize(pack_of_poker=1)
		@cards = []
		pack_of_poker.times {init_one_poker(@cards)}
	end
  
	#初始化一副扑克,处于未洗牌状态
	def init_one_poker(cards)
		CARD_TYPE.each do |type|
		  1.upto(10) do |x|
			cards<<"#{type}-#{x}"
		  end
		  cards<<"#{type}-J"
		  cards<<"#{type}-Q"
		  cards<<"#{type}-K"
		end
		cards<<"大鬼"   #Big Joker:大鬼
		cards<<"小鬼"   #Little Joker:小鬼
	end

	#将cards洗牌后均分成parts份
	def average_alloc(parts)
		raise "每垛牌数不是整数!" if @cards.count % parts != 0
		cards=@cards.shuffle
		pieces,rets = cards.size/parts,[]
		parts.times {|x| rets<<cards[x*pieces...(x+1)*pieces]}
		rets
	rescue=>e
		err(e)
	end
	
	#打印表示card的彩色字符串
	def self.show(card)
		case type(card)
			when "小鬼","黑桃","梅花"
				@@ct.color(card,Ctxt::ON_B_WHITE+Ctxt::BLACK)
			when "大鬼","红桃","方块"
				@@ct.color(card,Ctxt::ON_B_WHITE+Ctxt::B_RED)
			else
				raise "unknow type!!!"
		end
	end
  
	#返回一个表示card的彩色字符串
	def self.color_str(card)
		case type(card)
			when "小鬼","黑桃","梅花"
				@@ct.color_str(card,Ctxt::ON_B_WHITE+Ctxt::BLACK)
			when "大鬼","红桃","方块"
				@@ct.color_str(card,Ctxt::ON_B_WHITE+Ctxt::B_RED)
			else
				raise "unknow type!!!"
		end
	end
  
	#返回一张新card,其值是原有card绝对值+add
	def self.add_val(card,add)
		org_val = self.val(card)
		new_val = org_val + add

		type = type(card)
		type = "黑桃" if type == "小鬼"
		type = "红桃" if type == "大鬼"
		if new_val >=14
			#new_val = 14
			new_card = "大鬼"
		elsif new_val <= 0
			#new_val = 0
			new_card = "小鬼"
		elsif new_val == 13
			new_card="#{type}-K"
		elsif new_val == 12
			new_card="#{type}-Q"
		elsif new_val == 11
			new_card="#{type}-J"
		else
			new_card = "#{type}-#{new_val}"
		end
		new_card
	end
  
	#返回card值的绝对值
	def self.val(card)
		return 14 if card == "大鬼"
		return 0 if card == "小鬼"
		return case card[3..-1]
		  when "K";13
		  when "Q";12
		  when "J";11
		  else card[3..-1].to_i
		end
	end
  
	#返回card的值
	def self.v1(card)
		return 14 if card == "大鬼"
		return -1 * self.v1("大鬼") if card == "小鬼"
		v = case card[3..-1]
			when "K";13
			when "Q";12
			when "J";11
			else card[3..-1].to_i
		end

		v *= -1 if type(card) == "黑桃" || type(card) == "梅花"
		v
	end
	
	#返回card的花型
	def self.type(card)
		card[0..1]
	end
end
